package ca.bcit.comp_3512.assignment_02.business_logic;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import ca.bcit.comp_3512.assignment_02.data_managers.UserManager;
import ca.bcit.comp_3512.assignment_02.data_managers.WeeklyEntryManager;
import ca.bcit.comp_3512.assignment_02.data_models.User;
import ca.bcit.comp_3512.assignment_02.data_models.WeeklyEntry;

@Named("loggedIn")
@SessionScoped

public class LoggedIn implements Serializable
{
    private Integer userID;
    private String password;
    private User employee;
    
    private Integer weekNumber;
    private Integer yearNumber;
    private String[] dateList;
    private String selectedDate;
    @Inject UserManager userManager;
    //@Inject WeeklyEntryManager weeklyEntryManager;
    @Inject NewWeeklyEntry week;
    //@Inject NewProjectRow projectRow;    
    private String successPage = "menu";
    private String loginPage = "login";
    
    public LoggedIn() {
        GregorianCalendar x = new GregorianCalendar();
        weekNumber = x.get(GregorianCalendar.WEEK_OF_YEAR);
        yearNumber = x.get(GregorianCalendar.YEAR);
        //loadDateList(); 
        
    }
    
    public ArrayList<String> loadDateList()
//    public String[] loadDateList()
    {
        ArrayList<String> temp = new ArrayList<String>();
        for (int i = 0; i < employee.getWeeklyEntries().size(); ++i)
        {
            temp.add(employee.getWeeklyEntries().get(i).getYear().toString() + "-" + employee.getWeeklyEntries().get(i).getWeek().toString());
        }
        return temp;
//        return (String[]) employee.getWeeks().keySet().toArray();
    }
    
    public boolean dateListEmpty(){
        return employee.getWeeklyEntries().size() == 0;
    }
    
    public String validate()
    {
        employee = userManager.find(userID);
        if (employee != null && employee.getPassword().equals(password))
        {
            return successPage;
        }
        userID = null;
        password = null;
        return loginPage;
    }
    
//    public Double getWeeklyTotal()
//    {
//        return employee.getWeeks().get(yearNumber + "-" + weekNumber).getWeeklyTotal();
//    }
//    
//    public Double getMondayTotal()
//    {
//        return employee.getWeeks().get(yearNumber + "-" + weekNumber).getMondayTotal();
//    }
//    
//    public Double getTuesdayTotal()
//    {
//        return employee.getWeeks().get(yearNumber + "-" + weekNumber).getTuesdayTotal();
//    }
//    
//    public Double getWednesdayTotal()
//    {
//        return employee.getWeeks().get(yearNumber + "-" + weekNumber).getWednesdayTotal();
//    }
//    
//    public Double getThursdayTotal()
//    {
//        return employee.getWeeks().get(yearNumber + "-" + weekNumber).getThursdayTotal();
//    }
//    
//    public Double getFridayTotal()
//    {
//        return employee.getWeeks().get(yearNumber + "-" + weekNumber).getFridayTotal();
//    }
//    
//    public Double getSaturdayTotal()
//    {
//        return employee.getWeeks().get(yearNumber + "-" + weekNumber).getSaturdayTotal();
//    }
//    
//    public Double getSundayTotal()
//    {
//        return employee.getWeeks().get(yearNumber + "-" + weekNumber).getSundayTotal();
//    }
//    
//    public List<ProjectRow> getProjects()
//    {
//        return new ArrayList<ProjectRow>(employee.getWeeks().get(yearNumber + "-" + weekNumber).getProjects().values());
//    }
    
    public String addWeek()
    {
//        WeeklyEntry temp = new WeeklyEntry();
//        temp.setUser(employee);
//        temp.setUserID(employee.getUserID());
//        temp.setProjectID(projectID));
//        
//        weeklyEntryManager.merge(temp);
//        
//        employee.getWeeklyEntries()
//        if(!employee.getWeeklyEntries().contains(employee..containsKey(yearNumber+"-"+weekNumber)){
//            week.getWeek().setWeekID(weekNumber);
//            week.getWeek().setYearID(yearNumber);
//            employee.getWeeks().put(yearNumber + "-" + weekNumber, week.getWeek());            
//        }
//        
//        //loadDateList();
        return "schedule";
    }
//
//    public String addRow()
//    {
//        employee.getWeeks().get(yearNumber + "-" + weekNumber).getProjects().put(projectRow.getProjectRow().getUniqueKey(), projectRow.getProjectRow());
//        projectRow.clearRow();
//        return "success?faces-redirect=true";
//    }
    
    public Integer getUserID()
    {
        return userID;
    }

    public void setUserID(Integer userID)
    {
        this.userID = userID;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public User getEmployee()
    {
        return employee;
    }

    public void setEmployee(User employee)
    {
        this.employee = employee;
    }

    /**
     * @return the weekNumber
     */
    public Integer getWeekNumber() {
        return weekNumber;
    }

    /**
     * @return the yearNumber
     */
    public Integer getYearNumber() {
        return yearNumber;
    }

    /**
     * @param weekNumber the weekNumber to set
     */
    public void setWeekNumber(int weekNumber) {
        this.weekNumber = weekNumber;
    }

    /**
     * @param yearNumber the yearNumber to set
     */
    public void setYearNumber(Integer yearNumber) {
        this.yearNumber = yearNumber;
    }
    public String logout(){
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        userID = null;
        password = null;
        return "login";
    }

    public String[] getDateList() {
        System.out.println("----------------------------------------------------!!!!!!!!!!!!!!!!!!!!!!!");
        System.out.println(Arrays.toString(dateList));
        return dateList;
    }

    public void setDateList(String[] dateList) {
        this.dateList = dateList;
    }
    public String getSelectedDate() {
        return selectedDate;
    }
    public void setSelectedDate(String selectedDate) {
        this.selectedDate = selectedDate;
    }

    
}
